package com.datn.utils;

import com.datn.activity.R;
import com.datn.utils.places.Place;
import com.google.android.maps.GeoPoint;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.location.Location;
import android.preference.PreferenceManager;
import android.util.Log;

public class PreferenceUtils {
	
	private static PreferenceUtils instance;
	
	private static int resourceDefault = R.drawable.image_not_available;
	
	private static int LATITUDE_DEFAULT = 37422006;
	private static int LONGITUDE_DEFAULT = -122084095;
	
	private Context context;
	public static SharedPreferences preference;
	public static String IS_NOT_EXIST_PREFERENCE="isNotExistPreference";
	public static final String KEY_USERNAME="UsernamePre";
	public static final String KEY_PASSWORD="PasswordPre";
	public static final String KEY_ROLE="RolePre";
	
	public static final String KEY_CATEGORY="CategoryPlacePre";
	public static final String KEY_RESOURCE_CATEGORY="ResourceCategoryPre";
	
	public static final String KEY_URL="URLPre";
	public static final String KEY_ADDRESS="AddressPre";
	public static final String KEY_DISTANCE="DistancePre";
	public static final String KEY_NAME="NamePre";
	public static final String KEY_PHONE="PhonePre";
	public static final String KEY_LATITUDE="LatitudePre";
	public static final String KEY_LONGITUDE="LongitudePre";
	
	
	public static final String KEY_CURRENT_LATITUDE_LOCATION="CurrentLatitudeLocationOfUserPre";
	public static final String KEY_CURRENT_LONGITUDE_LOCATION="CurrentLongitudeLocationOfUserPre";
	
	public static final String KEY_MODE="OnlineOrOfflineModePre";
	
	
	public static final String KEY_SEARCH_TYPE ="SearchTypePre";
	public static final String KEY_VALUE_SEARCH_TYPE = "ValueSearchTypePre";
	
	public static PreferenceUtils getInstance(Context context){
		instance=new PreferenceUtils(context);
		return instance;
	}
	
	
	private PreferenceUtils(Context context){
		this.context=context;
		preference=PreferenceManager.getDefaultSharedPreferences(this.context);		
	}
	
	
	
	public boolean isExistStringPreference(String keyPreference){
		return preference.contains(keyPreference);
	}
	
	public static void setPreference(String keyPreference, String valuePreference){
			SharedPreferences.Editor editor=preference.edit();
        	editor.putString(keyPreference, valuePreference);
        	editor.commit();
	}
	
	
	
	public static String getPreference(String keyPreference){
		return preference.getString(keyPreference, "");
	}
	
	public static void setUser(String stUsername, String stPassword, String stRole){
		setPreference(KEY_USERNAME, stUsername);
		setPreference(KEY_PASSWORD, stPassword);
		setPreference(KEY_ROLE, stRole);
	}
	
	
	//CATEGORY
	public static void setCategory(String stCategory){
		setPreference(KEY_CATEGORY, stCategory);
	}
	
	public static String getCategory(){
		return getPreference(KEY_CATEGORY);
	}
	
	//PLACE DETAIL
	public static void setPlaceDetails(Place place){
		setPreference(KEY_URL, place.getActualurl());
		setPreference(KEY_ADDRESS, place.getAddressLine());
		setPreference(KEY_DISTANCE, place.getDistance());
		setPreference(KEY_NAME, place.getName());
		setPreference(KEY_PHONE, place.getPhonenumber());
		setPreference(KEY_LATITUDE, String.valueOf(place.getLatitude()));
		setPreference(KEY_LONGITUDE, String.valueOf(place.getLongtitude()));		
	}
	
	public static Place getPlaceDetail(){
		Place place = new Place();
		place.setActualurl(getPreference(KEY_URL));
		place.setAddressLine(getPreference(KEY_ADDRESS));
		place.setDistance(getPreference(KEY_DISTANCE));
		place.setName(getPreference(KEY_NAME));
		place.setPhonenumber(getPreference(KEY_PHONE));
		try {
			place.setLatitude(Double.parseDouble(getPreference(KEY_LATITUDE)));
			place.setLongtitude(Double.parseDouble(getPreference(KEY_LONGITUDE)));
		} catch (Exception e) {
			// TODO: handle exception
			place.setLatitude(-1.0);
			place.setLongtitude(-1.0);
		}
		return place;
	}
	
	public static void setCurrentLocation(GeoPoint current){
		if (current!=null)
			{
				setPreference(KEY_CURRENT_LATITUDE_LOCATION, String.valueOf(current.getLatitudeE6()));
				setPreference(KEY_CURRENT_LONGITUDE_LOCATION, String.valueOf(current.getLongitudeE6()));
			}
	}
	
	public static GeoPoint getCurrentLocation(){
		try {
			int lat= Integer.parseInt(getPreference(KEY_CURRENT_LATITUDE_LOCATION));
			int lng= Integer.parseInt(getPreference(KEY_CURRENT_LONGITUDE_LOCATION));
			return new GeoPoint(lat, lng);
		} catch (Exception e) {
			// TODO: handle exception
			return new GeoPoint(LATITUDE_DEFAULT, LONGITUDE_DEFAULT);
		}
	}
	
	private static void setMode(String mode){
		setPreference(KEY_MODE, mode);
	}
	
	private static String getMode(){
		return getPreference(KEY_MODE);
	}
	
	public static void setModeOnlinẹ̣(boolean isModeOnline){
		if (isModeOnline) setMode("ONLINE");
		else setMode("OFFLINE");
	}
	
	public static boolean isModeOnline(){
		if (getPreference(KEY_MODE)!=null){
			if (getPreference(KEY_MODE).equalsIgnoreCase("ONLINE")){
				return true;
			}
		}
		return false;
	}
	
	public static void setResourceCategory(int resource){
		try {
			String stResource = String.valueOf(resource);
			
			setPreference(KEY_RESOURCE_CATEGORY, stResource);
		} catch (Exception e) {
			// TODO: handle exception
			setPreference(KEY_RESOURCE_CATEGORY, String.valueOf(resourceDefault));
		}	
	}
	
	public static int getResourceCategory(){
		String stResource = getPreference(KEY_RESOURCE_CATEGORY);
		if (stResource!=null){
			if (!stResource.trim().equalsIgnoreCase("")){
				try {
					return Integer.parseInt(stResource);
				} catch (Exception e) {
					// TODO: handle exception
					return resourceDefault;
				}
			}
		}
		return resourceDefault;
		
	}
	
	
	public static void setSearchTypeAnyAddress(boolean isAnyAddress){
		if (isAnyAddress){
			PreferenceUtils.setPreference(KEY_SEARCH_TYPE, "ANY_ADDRESS");
		}
		else {
			PreferenceUtils.setPreference(KEY_SEARCH_TYPE, "CATEGORY_ADDRESS");
		}
	}
	
	public static boolean isSearchTypeAnyAddress(){
		if (PreferenceUtils.getPreference(KEY_SEARCH_TYPE)!=null){
			if (PreferenceUtils.getPreference(KEY_SEARCH_TYPE).equalsIgnoreCase("ANY_ADDRESS")){
				return true;
			}
		}
		return false;
	}
	
	
	public static void setSearchTypeValue(String valuePreference){
		PreferenceUtils.setPreference(KEY_VALUE_SEARCH_TYPE, valuePreference);
	}
	
	public static String getSearchTypeValue(){
		String stValue = PreferenceUtils.getPreference(KEY_VALUE_SEARCH_TYPE);
		if (stValue!=null){
			if (!stValue.trim().equalsIgnoreCase("")){
				return stValue;
			}
		}
		return "";
	}
	
	
	
	
}
